package com.hmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmall.common.utils.BeanUtils;
import com.hmall.common.utils.UserContext;
import com.hmall.domain.dto.OrderFormDTO;
import com.hmall.domain.po.Order;
import com.hmall.domain.po.OrderDetail;
import com.hmall.domain.vo.OrderVO;
import com.hmall.service.IOrderDetailService;
import com.hmall.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
@Slf4j
public class OrderController {
    private final IOrderService orderService;
    private final IOrderDetailService orderDetailService;


    /**
     * 查看该用户所有的下单的商品名称
     * @return 返回下单的额商品名称
     */
    @ApiOperation("查询当前用户的所有订单")
    @GetMapping()
    public List<String> queryOrderAll(){
        // 1.获取当前用户id
        Long userId = UserContext.getUser();

        // 2.去order表里面获取所有的订单id
        // 2.1构建查询语句,获取该用户的所有订单
        QueryWrapper<Order> queryOrderIdsWrapper = new QueryWrapper<>();
        queryOrderIdsWrapper.eq("user_id",userId);
        List<Order> orders = orderService.list(queryOrderIdsWrapper);
        // 2.2在订单中，提取订单id
        List<Long> orderIds = new ArrayList<>();
        for(Order o : orders){
            orderIds.add(o.getId());
        }

        // 3.根据订单id去order_detail表里面获取所有的商品名称name
        List<String> orderNames = new ArrayList<>();
        for(Long orderId : orderIds){
            // 3.1构建查询条件
            QueryWrapper<OrderDetail> queryOrderDeatilWrapper = new QueryWrapper<>();
            queryOrderDeatilWrapper.eq("order_id",orderId);
            // 3.2根据订单id去查询到商品名称
            List<OrderDetail> orderDetails = orderDetailService.list(queryOrderDeatilWrapper);
            // 3.3将商品名称保存到list里面
            for(OrderDetail detail :orderDetails){
                orderNames.add(detail.getName());
            }
        }

        // 4.构建返回
        log.info("用户【{}】买了{}",userId,orderNames);
        return orderNames;
    }



    @ApiOperation("根据id查询订单")
    @GetMapping("{id}")
    public OrderVO queryOrderById(@Param ("订单id")@PathVariable("id") Long orderId) {
        log.info("id为【{}】的订单信息是：{}",orderId,orderService.getById(orderId));
        return BeanUtils.copyBean(orderService.getById(orderId), OrderVO.class);
    }

    @ApiOperation("创建订单")
    @PostMapping
    public Long createOrder(@RequestBody OrderFormDTO orderFormDTO){
        return orderService.createOrder(orderFormDTO);
    }

    @ApiOperation("标记订单已支付")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PutMapping("/{orderId}")
    public void markOrderPaySuccess(@PathVariable("orderId") Long orderId) {
        orderService.markOrderPaySuccess(orderId);
    }
}
